/**
 * 初始化媒介详情对话框
 */
var MediumInfoDlg = {
    mediumInfoData: {},
    zTreeInstance: null,
    validateFields: {
        mediumname: {
            validators: {
                notEmpty: {
                    message: '媒介名称不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '媒介名称长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        mName: {
            validators: {
                notEmpty: {
                    message: '上级名称不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
MediumInfoDlg.clearData = function () {
    this.mediumInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MediumInfoDlg.set = function (key, val) {
    this.mediumInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MediumInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
MediumInfoDlg.close = function () {
    parent.layer.close(window.parent.Medium.layerIndex);
}

/**
 * 点击媒介ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
MediumInfoDlg.onClickMedium = function (e, treeId, treeNode) {
    $("#mName").attr("value", MediumInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
}

/**
 * 显示媒介选择的树
 *
 * @returns
 */
MediumInfoDlg.showMediumSelectTree = function () {
    var mName = $("#mName");
    var mNameOffset = $("#mName").offset();
    $("#parentMediumMenu").css({
        left: mNameOffset.left + "px",
        top: mNameOffset.top + mName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏媒介选择的树
 */
MediumInfoDlg.hideMediumSelectTree = function () {
    $("#parentMediumMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
MediumInfoDlg.collectData = function () {
    this.set('id').set('mediumname').set('remarks').set('num').set('pid');
}

/**
 * 验证数据是否为空
 */
MediumInfoDlg.validate = function () {
    $('#mediumInfoForm').data("bootstrapValidator").resetForm();
    $('#mediumInfoForm').bootstrapValidator('validate');
    return $("#mediumInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
MediumInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/medium/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Medium.table.refresh();
        MediumInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.mediumInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
MediumInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/medium/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Medium.table.refresh();
        MediumInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.mediumInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentMediumMenu" || $(
            event.target).parents("#parentMediumMenu").length > 0)) {
        MediumInfoDlg.hideMediumSelectTree();
    }
}

$(function () {
    Feng.initValidator("mediumInfoForm", MediumInfoDlg.validateFields);

    var ztree = new $ZTree("parentMediumMenuTree", "/medium/tree");
    ztree.bindOnClick(MediumInfoDlg.onClickMedium);
    ztree.init();
    MediumInfoDlg.zTreeInstance = ztree;
});
